package org.intertwine.rssbot;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;

/**
 * Created by IntelliJ IDEA.
 * User: Stian Lindhom
 * Date: 21.feb.2010
 * Time: 16:45:47
 * To change this template use File | Settings | File Templates.
 */
public class RssReaders implements FeedsChangedListener {

    private static Map<String, RssReader> rssReaders = new HashMap<String, RssReader>();
    private Timer t;
    private NewRSSItemListener nr;

    public RssReaders(NewRSSItemListener nr) {
        this.nr = nr;
        t = new Timer();
    }

    public void onFeedAdded(Feed f) {
        RssReader rs = new RssReader(f, nr);
        rssReaders.put(f.getFeedUrl(), rs);
        t.scheduleAtFixedRate(rs, 100, 1000*60);
    }

    public void onFeedRemoved(Feed f) {
        RssReader r = rssReaders.remove(f.getFeedUrl());
        if (r != null) {
            if (r.cancel()) {
                System.out.println(f + " cancelled");
            }
        }
    }
}
